package com.ayu.wxgzh.ayucyy.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ayu.wxgzh.ayucyy.Utils.HttpUtils;
import com.ayu.wxgzh.ayucyy.entity.Article;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 徐林玉
 * @Description NewsService
 * @Date 2022/8/31 9:22
 * @Version 1.0
 */
@Slf4j
public class NewsService {


    private static String host = "https://ali-news.showapi.com";
    private static String path = "/newsList";
    private static String method = "GET";
    private static String appcode = "25882f02054e47f9923086afd3ee4cc6";
    private static List<String> channelIdList = Arrays.asList(
            "5572a109b3cdc86cf39001db",
            "5572a109b3cdc86cf39001de",
            "5572a108b3cdc86cf39001d6",
            "5572a10ab3cdc86cf39001ec",
            "5572a10ab3cdc86cf39001ee",
            "5572a10bb3cdc86cf39001f5");

    public static Article getRandomNews() {

        Article article = null;
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        //随机选择频道
        int randomInt = getRandomInt(0, channelIdList.size() - 1);

        log.info("新闻频道随机数:"+randomInt);
        querys.put("channelId", channelIdList.get(randomInt));
        querys.put("maxResult", "20");
        querys.put("needAllList", "0");
        querys.put("needContent", "0");
        querys.put("needHtml", "0");
        querys.put("page", "1");

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);

            //获取response的body
            String contentStr = EntityUtils.toString(response.getEntity());

            JSONObject content = JSONObject.parseObject(contentStr);

            JSONArray contentlist = content.getJSONObject("showapi_res_body").getJSONObject("pagebean").getJSONArray("contentlist");

            if(!CollectionUtils.isEmpty(contentlist)){
                //随机获取新闻
                int r2 = getRandomInt(0, contentlist.size() - 1);
                log.info("新闻随机数:"+r2);
                JSONObject randomNews = (JSONObject)contentlist.get(r2);

                //组装新闻
                article = new Article();
                article.setTitle(randomNews.getString("title"));
                article.setPicUrl(randomNews.getString("img"));
                article.setUrl(randomNews.getString("link"));
                article.setDescription(randomNews.getString("desc"));
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return article;
    }


    public static int getRandomInt(int start, int end) {

        int res = (int) (start + Math.random() * (end - start + 1));
        return res;
    }


}
